/**
 * 对目标文件进行更新, 并提交到本地仓库.
 */

import fs from 'fs';
import path from 'path';
import { Response } from 'express';

async function updateTargetFile(
	res: Response,
	{
		// commit参数.
		commit_options = {},
	} = {}
) {
	try {
		fs.writeFileSync(
			path.resolve(__dirname, '../gitProject/commitProject/index.txt'),
			Date.now().toString()
		);
	} catch (e: any) {
		res.json(formatResJson({ code: 'Fail', msg: `更新目标文件内容失败:${e?.message}` }));
		return Promise.reject();
	}

	try {
		await git.add('.');
		await git.commit(moment_g().format('YYYY-MM-DD HH:mm:ss'), commit_options);
	} catch (e: any) {
		res.json(formatResJson({ code: 'Fail', msg: `提交目标文件内容失败:${e?.message}` }));
		return Promise.reject();
	}

	return Promise.resolve();
}

global.updateTargetFile = updateTargetFile;
